package com.kyubi.tagger.decorators;

import java.util.Vector;

import com.kyubi.tagger.ITagger;

/**
 * This class represents the Stemming decorator which implements the
 * PorterStemmer algorithm
 * @author anand
 *
 */
public class PorterStemmer implements ITagger {

	private ITagger tagger;
	private net.sf.snowball.ext.PorterStemmer stemmer;
	
	/**
	 * Constructor
	 * @param tagger	the tagger being decorated
	 */
	public PorterStemmer(ITagger tagger) {
		this.tagger = tagger;
		stemmer = new net.sf.snowball.ext.PorterStemmer();
	}
	
	/**
	 * Function to stem the given line
	 * @param line	the line to stem
	 * @return	the stemmed version of the line
	 */
	private String stemLine(String line) {
		String[] tokens = line.split("[ ]+");
		StringBuffer str = new StringBuffer();
		
		for(int i=0; i<tokens.length; i++) {
			stemmer.setCurrent(tokens[i]);
			stemmer.stem();
			str.append(stemmer.getCurrent() + " ");
		}
		
		return str.toString().trim();
	}
	
	/**
	 * @see com.kyubi.tagger.ITagger
	 */
	public Vector<String> getOriginalValues() {
		Vector<String> tValues = tagger.getOriginalValues();
		Vector<String> rValues = new Vector<String>();
		
		for(String value : tValues) {
			rValues.add(stemLine(value));
		}
		
		return rValues;
	}

	/**
	 * @see com.kyubi.tagger.ITagger
	 */
	public Vector<String> getTaggedValues() {
		Vector<String> tValues = tagger.getTaggedValues();
		Vector<String> rValues = new Vector<String>();
		
		for(String value : tValues) {
			rValues.add(stemLine(value));
		}
		
		return rValues;
	}

}
